package cn.wenhaha.app.user.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import cn.wenhaha.app.user.service.IUserService;
import cn.wenhaha.common.bean.dto.admin.UserLoginDTO;
import cn.wenhaha.common.bean.dto.admin.UserUpdatePwdDTO;
import cn.wenhaha.common.core.domain.AjaxV1Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;


/**
 * 测试控制器
 * --------
 *
 * @author ：wyndem
 * @Date ：Created in 2022-03-19 21:33
 */
@RestController
@RequestMapping("/api/v1/user")
public class ApiUserController {

    @Autowired
    private IUserService userService;

    @PostMapping("login")
    public AjaxV1Result login(@RequestBody @Valid UserLoginDTO loginDTO){
        return AjaxV1Result.newSuccess((Object)userService.login(loginDTO));
    }


    /**
     * <p>
     *    修改密码
     * </p>
     * @Author: Wyndem
     * @DateTime: 2023-01-26 19:21
     */
    @PostMapping("update/pwd")
    @SaCheckLogin
    public AjaxV1Result<Boolean> updatePwd(@RequestBody @Valid UserUpdatePwdDTO updateDTO){
        return AjaxV1Result.newSuccess(userService.updatePwd(updateDTO));
    }

    @PostMapping("info")
    @SaCheckLogin
    public AjaxV1Result info(){
        long id = StpUtil.getLoginIdAsLong();
        return AjaxV1Result.newSuccess(userService.info(id));
    }

    @PostMapping("logout")
    @SaCheckLogin
    public AjaxV1Result logout(){
        String tokenValue = StpUtil.getTokenValue();
        StpUtil.logoutByTokenValue(tokenValue);
        return AjaxV1Result.newSuccess();
    }



}
